﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Threading;
using System.Timers;

namespace SpriteEditor
{
    public partial class frmMain
    {
        private void FillDefautData()
        {
            spr = new Sprite();
            //currentModuleIdUsed = 999;
            //currentFrameIdUsed = 1999;
            //currentAnimIdUsed = 2999;

            //Init main form
            this.Text = "Sprite Editor";

            //Init image
            textBox_Filename.Text = "";

            //Init modules
            CurSel_Module = null;
            dataGridView_Modules.DataSource = null;

            //Init frames
            CurSel_Frame = null;
            dataGridView_Frames.DataSource = null;

            //Init animation
            CurSel_Animation = null;
            dataGridView_Animations.DataSource = null;
        }

        private void RemoveAll()
        {
            //Common vaiable
            spr = null;

            //moduleTasks = new ModuleSaveTask();

            //Modules editor

            root_Modules_Grid_Default_Pos = new Point(panel_Modules.Width / 2, panel_Modules.Height / 2);
            root_Modules_Grid_Pos = new Point(panel_Modules.Width / 2, panel_Modules.Height / 2);

            CurSel_Module = null;
            ModulePanelMouseDown = false;
            ModulePanelMouseDownPos = new Point();

            modules_editor_mode = PANEL_EDIT_RECT_MODE;

            modules_edit_state = MODULE_RESIZE_NONE;

            //Frame editor
            root_Frames_Grid_Default_Pos = new Point(panel_Frames.Width / 2, panel_Frames.Height / 2);
            root_Frames_Grid_Pos = new Point(panel_Frames.Width / 2, panel_Frames.Height / 2);

            CurSel_Frame = null;
            FramePanelMouseDown = false;
            FramePanelMouseDownPos = new Point();

            frames_editor_mode = PANEL_EDIT_RECT_MODE;

            frames_edit_state = FRAME_EDIT_NONE;

            //Animation editor
            root_Animations_Grid_Default_Pos = new Point(panel_Animations.Width / 2, panel_Animations.Height / 2);
            root_Animations_Grid_Pos = new Point(panel_Animations.Width / 2, panel_Animations.Height / 2);

            CurSel_Animation = null;
            AnimationPanelMouseDown = false;
            AnimationPanelMouseDownPos = new Point();

            animations_editor_mode = PANEL_EDIT_RECT_MODE;

            animations_edit_state = ANIMATION_EDIT_NONE;
        }

        private void ReFreshAll()
        {
            //Tab Control

            //Tab Module
            if (tabControl_Main.SelectedIndex == MAIN_TAB_MODULES_EDITOR)
            {
                if (spr == null)
                {
                    textBox_Filename.Text = null;
                    dataGridView_Modules.DataSource = null;
                }
                else
                {
                    textBox_Filename.Text = spr.spr_image_FileName;
                    dataGridView_Modules.DataSource = spr.lst_Modules;
                }
                DrawPanelModule();
            }

            //Tab Frame
            if (tabControl_Main.SelectedIndex == MAIN_TAB_FRAMES_EDITOR)
            {
                if (spr == null)
                {
                    dataGridView_Frames.DataSource = null;
                    dataGridView_ModulesOfFrame.DataSource = null;
                }
                else
                {
                    dataGridView_Frames.DataSource = spr.lst_Frames;
                    dataGridView_ModulesOfFrame.DataSource = null;
                }
                DrawPanelFrame();
                DrawFlowPanelFrame();
            }

            //Tab Animation
            if (tabControl_Main.SelectedIndex == MAIN_TAB_ANIMATIONS_EDITOR)
            {
                DrawPanelAnimation();
                DrawFlowPanelAnimation();
            }
        }

        const int LeftGroupSizeWith_Max = 500;
        private void ResizeForm()
        {
            //Tab Control
            tabControl_Main.Size = new Size(this.ClientSize.Width, this.ClientSize.Height - (105 - 28));

            //Tab Module
            if (tabControl_Main.SelectedIndex == MAIN_TAB_MODULES_EDITOR)
            {
                group_Image.Size = new Size(SpriteCalculation.Min((tab_Module.Width * 40) / 100, LeftGroupSizeWith_Max), group_Image.Height);
                textBox_Filename.Size = new Size(group_Image.Width - 20, textBox_Filename.Height);
                button_TranspImage.Location = new Point(group_Image.Width - 70, button_TranspImage.Location.Y);

                group_Modules.Size = new Size(SpriteCalculation.Min((tab_Module.Width * 40) / 100, LeftGroupSizeWith_Max), tab_Module.Height - 109);
                dataGridView_Modules.Size = new Size(group_Modules.Width - 20, group_Modules.Height - 60);
                button_DownModule.Location = new Point(group_Modules.Width - 60, button_DownModule.Location.Y);
                button_UpModule.Location = new Point(group_Modules.Width - 60 - 55, button_UpModule.Location.Y);

                groupBox_EditModules.Location = new Point(group_Image.Width + 20, groupBox_EditModules.Location.Y);
                groupBox_EditModules.Size = new Size(tab_Module.Width - group_Image.Width - 34, tab_Module.Height - 19);
                panel_Modules.Size = new Size(groupBox_EditModules.Width - 6, groupBox_EditModules.Height - 50);

                int dx = root_Modules_Grid_Pos.X - root_Modules_Grid_Default_Pos.X;
                int dy = root_Modules_Grid_Pos.Y - root_Modules_Grid_Default_Pos.Y;

                root_Modules_Grid_Pos = new Point(dx + panel_Modules.Width / 2,
                                             dy + panel_Modules.Height / 2);
                root_Modules_Grid_Pos_Scale = new Point((int)(root_Modules_Grid_Pos.X*m_ZoomInModule),(int)(root_Modules_Grid_Pos.Y*m_ZoomInModule));
                root_Modules_Grid_Default_Pos = new Point(panel_Modules.Width / 2, panel_Modules.Height / 2);

                //DrawPanelModule();
            }

            //Tab Frame
            if (tabControl_Main.SelectedIndex == MAIN_TAB_FRAMES_EDITOR)
            {
                group_Frames.Size = new Size(SpriteCalculation.Min((tab_Frame.Width * 40) / 100, LeftGroupSizeWith_Max), (tab_Frame.Height - 29) * 50 / 100);
                button_DownFrame.Location = new Point(group_Frames.Width - 60, button_DownFrame.Location.Y);
                button_UpFrame.Location = new Point(group_Frames.Width - 60 - 55, button_UpFrame.Location.Y);
                dataGridView_Frames.Size = new Size(group_Frames.Width - 20, group_Frames.Height - 60);

                group_ModulesOfFrame.Location = new Point(group_ModulesOfFrame.Location.X, group_Frames.Height + 20);
                group_ModulesOfFrame.Size = new Size(SpriteCalculation.Min((tab_Frame.Width * 40) / 100, LeftGroupSizeWith_Max), (tab_Frame.Height - 29) * 50 / 100);
                button_DownModuleOfFrame.Location = new Point(group_ModulesOfFrame.Width - 60, button_DownModuleOfFrame.Location.Y);
                button_UpModuleOfFrame.Location = new Point(group_ModulesOfFrame.Width - 60 - 55, button_UpModuleOfFrame.Location.Y);
                dataGridView_ModulesOfFrame.Size = new Size(group_ModulesOfFrame.Width - 20, group_ModulesOfFrame.Height - 60);

                groupBox_EditFrames.Location = new Point(group_Frames.Width + 20, groupBox_EditFrames.Location.Y);
                groupBox_EditFrames.Size = new Size(tab_Frame.Width - group_Frames.Width - 34, tab_Frame.Height - 19);
                panel_Frames.Size = new Size(groupBox_EditFrames.Width - 6, groupBox_EditFrames.Height - 160);
                flowLayoutPanel_Frame.Location = new Point(flowLayoutPanel_Frame.Location.X, panel_Frames.Height + 20);
                flowLayoutPanel_Frame.Size = new Size(groupBox_EditFrames.Width - 6, flowLayoutPanel_Frame.Height);

                int dx = root_Frames_Grid_Pos.X - root_Frames_Grid_Default_Pos.X;
                int dy = root_Frames_Grid_Pos.Y - root_Frames_Grid_Default_Pos.Y;

                root_Frames_Grid_Pos = new Point(dx + panel_Frames.Width / 2,
                                             dy + panel_Frames.Height / 2);
                root_Frames_Grid_Default_Pos = new Point(panel_Frames.Width / 2, panel_Frames.Height / 2);

                //DrawPanelFrame();
                //DrawFlowPanelFrame();
            }

            //Tab Animation
            if (tabControl_Main.SelectedIndex == MAIN_TAB_ANIMATIONS_EDITOR)
            {
                group_Animations.Size = new Size(SpriteCalculation.Min((tab_Animation.Width * 40) / 100, LeftGroupSizeWith_Max), (tab_Animation.Height - 29) * 50 / 100);
                button_DownAnimation.Location = new Point(group_Animations.Width - 60, button_DownAnimation.Location.Y);
                button_UpAnimation.Location = new Point(group_Animations.Width - 60 - 55, button_UpAnimation.Location.Y);
                dataGridView_Animations.Size = new Size(group_Animations.Width - 20, group_Animations.Height - 60);

                group_FramesOfAnimation.Location = new Point(group_FramesOfAnimation.Location.X, group_Animations.Height + 20);
                group_FramesOfAnimation.Size = new Size(SpriteCalculation.Min((tab_Animation.Width * 40) / 100, LeftGroupSizeWith_Max), (tab_Animation.Height - 29) * 50 / 100);
                button_DownFrameOfAnimation.Location = new Point(group_FramesOfAnimation.Width - 60, button_DownFrameOfAnimation.Location.Y);
                button_UpFrameOfAnimation.Location = new Point(group_FramesOfAnimation.Width - 60 - 55, button_UpFrameOfAnimation.Location.Y);
                dataGridView_FramesOfAnimation.Size = new Size(group_FramesOfAnimation.Width - 20, group_FramesOfAnimation.Height - 60);

                groupBox_EditAnimations.Location = new Point(group_Animations.Width + 20, groupBox_EditAnimations.Location.Y);
                groupBox_EditAnimations.Size = new Size(tab_Animation.Width - group_Animations.Width - 34, tab_Animation.Height - 19);
                panel_Animations.Size = new Size(groupBox_EditAnimations.Width - 6, groupBox_EditAnimations.Height - 200);
                flowLayoutPanel_Animation.Location = new Point(flowLayoutPanel_Animation.Location.X, panel_Animations.Height + 60);
                flowLayoutPanel_Animation.Size = new Size(groupBox_EditAnimations.Width - 6, flowLayoutPanel_Animation.Height);
                button_PlayAnim.Location = new Point(groupBox_EditAnimations.Width - 70, button_PlayAnim.Location.Y);
                numericUpDown_FPS.Location = new Point(groupBox_EditAnimations.Width - 140, numericUpDown_FPS.Location.Y);
                label_FPS.Location = new Point(groupBox_EditAnimations.Width - 170, label_FPS.Location.Y);

                int dx = root_Animations_Grid_Pos.X - root_Animations_Grid_Default_Pos.X;
                int dy = root_Animations_Grid_Pos.Y - root_Animations_Grid_Default_Pos.Y;

                root_Animations_Grid_Pos = new Point(dx + panel_Animations.Width / 2,
                                             dy + panel_Animations.Height / 2);
                root_Animations_Grid_Default_Pos = new Point(panel_Animations.Width / 2, panel_Animations.Height / 2);

                //DrawPanelAnimation();
                //DrawFlowPanelAnimation();
            }
        }

        private void LoadSprite()
        {
            if (openFileDialog_LoadSprite.ShowDialog() == DialogResult.OK)
            {
                RemoveAll();
                ReFreshAll();
                FillDefautData();

                spr.sprite_FileName = openFileDialog_LoadSprite.FileName;
                ImportSprite(spr);

                //DrawPanelModule();
            }

            if (spr.sprite_FileName == null)
                this.Text = "Sprite Editor";
            else
                this.Text = "Sprite Editor - " + spr.sprite_FileName;

            if (tabControl_Main.SelectedIndex == MAIN_TAB_FRAMES_EDITOR)
            {
                DrawFlowPanelFrame();
            }
            else if (tabControl_Main.SelectedIndex == MAIN_TAB_ANIMATIONS_EDITOR)
            {
                DrawFlowPanelAnimation();
            }
        }

        private void LoadSprite(String spr_fileName)
        {
            if (File.Exists(spr_fileName))
            {
                RemoveAll();
                ReFreshAll();
                FillDefautData();

                spr.sprite_FileName = spr_fileName;
                ImportSprite(spr);

                if (spr.sprite_FileName == null)
                    this.Text = "Sprite Editor";
                else
                    this.Text = "Sprite Editor - " + spr.sprite_FileName;
            }
            else
            {
                MessageBox.Show("File Image is not found or not set in sprt file!!!");
            }
        }

        public void SaveSprite()
        {
            if (spr != null)
            {
                if (spr.sprite_FileName == null)
                {
                    if (saveFileDialog_SaveSprite.ShowDialog() == DialogResult.OK)
                    {
                        spr.sprite_FileName = saveFileDialog_SaveSprite.FileName;
                        ExportSprite(spr);
                    }
                }
                else
                {
                    ExportSprite(spr);
                }

                if (spr.sprite_FileName == null)
                    this.Text = "Sprite Editor";
                else
                    this.Text = "Sprite Editor - " + spr.sprite_FileName;
            }
            else
            {
                MessageBox.Show("Can not save. There is no data to save !!!");
            }
        }

        public void ExportSprite(Sprite spr)
        {
            if (spr != null && spr.sprite_FileName != null)
            {
                XmlEngine.Export(spr);
            }
            else
            {
                MessageBox.Show("Can not save. Invalid file name !!!");
            }
        }

        public void ImportSprite(Sprite spr)
        {
            if (spr != null && spr.sprite_FileName != null)
            {
                try
                {
                    //currentModuleIdUsed = 999;
                    //currentFrameIdUsed = 1999;
                    //currentAnimIdUsed = 2999;
                    if (XmlEngine.Import(spr) == 0)
                    {
                        spr = new Sprite();
                        //FillDefautData();
                        return;
                    }

                    if (SpriteCalculation.isRelativePath(spr.spr_image_FileName))
                    {
                        int temp = spr.sprite_FileName.LastIndexOf("\\");
                        String pathDirectorystr = "";
                        if (temp > 0)
                            pathDirectorystr = spr.sprite_FileName.Substring(0, temp + 1);

                        //Import image
                        if (File.Exists(pathDirectorystr + spr.spr_image_FileName))
                        {
                            Image tmp_img = Image.FromFile(pathDirectorystr + spr.spr_image_FileName);
                            spr.spr_image = new Bitmap(tmp_img);
                            tmp_img.Dispose();

                            textBox_Filename.Text = spr.spr_image_FileName;
                        }
                        else
                        {
                            MessageBox.Show("File is not found !!!");
                            return;
                        }
                    }
                    else
                    {
                        if (File.Exists(spr.spr_image_FileName))
                        {
                            //Import image
                            Image tmp_img = Image.FromFile(spr.spr_image_FileName);
                            spr.spr_image = new Bitmap(tmp_img);
                            tmp_img.Dispose();

                            textBox_Filename.Text = spr.spr_image_FileName;
                        }
                        else
                        {
                            MessageBox.Show("File is not found !!!");
                            return;
                        }
                    }

                    UpdateListModuleImages();
                    UpdateListFrameImages();

                    //Import modules
                    dataGridView_Modules.DataSource = spr.lst_Modules;

                    //Import frames
                    dataGridView_Frames.DataSource = spr.lst_Frames;

                    //Import animation
                    dataGridView_Animations.DataSource = spr.lst_Animations;
                    //if (spr.lst_Modules != null)
                    //    currentModuleIdUsed += spr.lst_Modules.Count;
                    //if (spr.lst_Frames != null)
                    //    currentFrameIdUsed += spr.lst_Frames.Count;
                    //if (spr.lst_Animations != null)
                    //    currentAnimIdUsed += spr.lst_Animations.Count;
                }
                catch (FileNotFoundException)
                {
                    MessageBox.Show("Can not load !!!");
                    FillDefautData();
                }
            }
            else
            {
                MessageBox.Show("Can not load !!!");
            }
        }

        public void UpdateListModuleImages()
        {
            if (spr != null && spr.spr_image != null && spr.lst_Modules != null)
            {
                for (int i = 0; i < spr.lst_Modules.Count; i++)
                {
                    spr.lst_Modules[i].GenerateImage(spr.spr_image);
                }
            }
        }

        public void UpdateListFrameImages()
        {
            if (spr != null && spr.spr_image != null && spr.lst_Frames != null)
            {
                for (int i = 0; i < spr.lst_Frames.Count; i++)
                {
                    spr.lst_Frames[i].GenerateImage();
                }
            }
        }
    }
}